// pages/getUserDetail/getUserDetail.js
var app = getApp();
//var address = require('../../utils/city.js')
var conmon = require('../../utils/conmon.js')
var animation
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modelHidden: true,
    areaInfo: "请选择",
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    queryId: '',
    isClick: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
        });
      }
    });
  },

  checkimg: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        console.log(res);
        if (res.tempFiles[0].size > 5120000) {
          wx.showToast({
            title: '上传图片不得大于5M',
            icon: 'loading'
          })
        } else {
          that.setData({
            avatarUrl: res.tempFilePaths[0],
            tempFiles: res.tempFiles[0].path
          })
        }
      },
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

  , userNameInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        userNameInput: e.detail.value
      })
    }
  }
  , shopNameInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        shopNameInput: e.detail.value
      })
    }
  }
  , IdCardInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        IdCardInput: e.detail.value
      })
    }
  }
  , phoneInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        phoneInput: e.detail.value
      })
    }
  }
  , productClassify: function (e) {
    if (e.detail.value != '') {
      this.setData({
        productClassify: e.detail.value
      })
    }
  }
  , addressDetailInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        addressDetailInput: e.detail.value
      })
    }
  },
  shopInfo: function (e) {
    if (e.detail.value != '') {
      this.setData({
        shopInfo: e.detail.value
      })
    }
  },
  dotimeInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        dotimeInput: e.detail.value
      })
    }
  },
  shopAddressInput:function(e){
    if (e.detail.value != '') {
      this.setData({
        shopAddress: e.detail.value
      })
    }
  },
  getAddressByWx: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          shopAddress: res.address,
          shopLat: res.latitude,
          shopLon: res.longitude
        })
      },
    })
  }
  , sendMsgToBack: function () {
    var that = this;
    if (this.data.isClick) {
      that.setData({
        isClick: false
      })
      setTimeout(function(){
        that.setData({
          isClick: true
        })
      },6000)
    }else{
      return
    }
 
    var reg = /^1[34578]\d{9}$/
    //  console.log(that.data.phoneInput);
    if (that.data.shopNameInput && that.data.userNameInput && that.data.IdCardInput && that.data.phoneInput && that.data.productClassify && that.data.shopAddress && that.data.shopInfo) {
      if (!reg.test(that.data.phoneInput)) {
        wx.showToast({
          title: "请输入正确的手机号",
          icon: 'loading',
          duration: 2000
        })
        return;
      }
      if (conmon.IdentityCodeValid(that.data.IdCardInput) != true) {
        wx.showToast({
          title: "请输入正确的身份证号",
          icon: 'loading',
          duration: 2000
        })
        return;
      }
    } else {
      wx.showToast({
        title: '请填写完整信息!',
        icon: "loading"
      })
      return;
    }
    wx.uploadFile({
      url: app.globalData.allUrl + "/shop/upload",
      filePath: that.data.tempFiles,
      name: 'file',
      header: { "Content-Type": "multipart/form-data" },
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'user': 'test'
      },
      success: function (rese) {
        console.log(rese);
        wx.request({
          url: app.globalData.allUrl + "/shop/try_shop",
          data: {
            shop_name: that.data.shopNameInput,
            user_name: that.data.userNameInput,
            id_card: that.data.IdCardInput,
            phone: that.data.phoneInput,
            product: that.data.productClassify,
            address: that.data.shopAddress,
            brief: that.data.shopInfo,
            userId: wx.getStorageSync("userid"),
            gps_j: that.data.shopLon,
            gps_w: that.data.shopLat,
            dotime: that.data.dotimeInput,
            image: rese.data
          },
          header: { 'content-type': "application/x-www-form-urlencoded" },
          method: 'POST',
          // header: {}, // 设置请求的 header
          success: function (res) {
            console.log(res);
            if (res.data == 200) {
              that.setData({
                modelHidden: false
              })
              setTimeout(function () {
                that.setData({
                  modelHidden: true
                })
                wx.switchTab({
                  url: '../../pages/homepage/homepage',
                })
              }, 2000)
            } else if (res.data == 300){
              wx.showModal({
                title: '提示',
                content: '您已申请过商家入驻，请勿重复提交！',
              })
            }else{
              wx.showModal({
                title: '提示',
                content: '您已申请过商家入驻，请勿重复提交！',
              })
            }
          }
        })
      }
    })
  },
  cityChange: function (e) {
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
      queryId: that.data.citys[value[2]].id
    })
  },
  hideCitySelected: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  startAnimation: function (isShow, offset) {
    var that = this
    var offsetTem
    if (offset == 0) {
      offsetTem = offset
    } else {
      offsetTem = offset + 'rpx'
    }
    this.animation.translateY(offset).step()
    this.setData({
      animationData: this.animation.export(),
      isVisible: isShow
    })
  }
})

var image = '';
function selectImage(file) { //显示图片
  if (!file.files || !file.files[0]) {
    return;
  }
  var reader = new FileReader();
  reader.onload = function (evt) {
    document.getElementById('image').src = evt.target.result;
    image = evt.target.result;
  };
  reader.readAsDataURL(file.files[0]);
}